package view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.Conference;
import model.FileParser;
import model.Role;
import model.State;

/**
 * Class to create a new conference JPanel
 * @author Sarah George, Kevin Anderson
 * @version Autumn 2012
 */
@SuppressWarnings("serial")
public class NewConfPanel extends JPanel {
	
	/**
	 * Field to hold the current state of the system.
	 */
	private State my_state;
	
	/**
	 * EXPOSING TEMPORARILY TO GET STATE
	 */
	public JFrame my_frame;
	
	/**
	 * Field to hold the conference name.
	 */
	private JTextField cname_field;
	
	/**
	 * Field to hold the start date.
	 */
	private JTextField sdate_field;
	
	/**
	 * Field to hold the end date.
	 */
	private JTextField edate_field;
	
	/**
	 * Field to hold the submission deadline.
	 */
	private JTextField subdl_field;
	
	/**
	 * Field to hold the review deadline.
	 */
	private JTextField revdl_field;
	
	/**
	 * Field to hold the recommendation deadline.
	 */
	private JTextField recdl_field;
	
	/**
	 * Field to hold the decision deadline.
	 */
	private JTextField decdl_field;
	
	/**
	 * Field to hold the conference location.
	 */
	private JTextField loc_field;
	
	/**
	 * Field to hold the size of the conference.
	 */
	private JTextField size_field;
	
	/**
	 * Constructor for a new conference panel.
	 * @param the_state the current state.
	 * @param the_frame the GUIView JFrame.
	 */
	public NewConfPanel(State the_state, JFrame the_frame) {
		super();
		my_state = the_state;
		my_frame = the_frame;
		setup();
	}

	/**
	 * Method to setup the new conference Panel.
	 */
	public void setup() {
		GridLayout exp_layout = new GridLayout(0,2);
		JLabel conf_name = new JLabel("Conference Name");
		cname_field = new JTextField();

		JLabel start_date = new JLabel("Start Date");
		sdate_field = new JTextField();
		sdate_field.setText("Tue/12/04/16/15/00/PST/2012");

		JLabel end_date = new JLabel("End Date");
		edate_field = new JTextField();
		edate_field.setText("Tue/12/04/16/15/00/PST/2012");

		JLabel sub_date = new JLabel("Paper Submission Deadline");
		subdl_field = new JTextField();
		subdl_field.setText("Tue/12/04/16/15/00/PST/2012");

		JLabel rdl_date = new JLabel("Review Submission Deadline");
		revdl_field = new JTextField();
		revdl_field.setText("Tue/12/04/16/15/00/PST/2012");

		JLabel recdl_date = new JLabel("Recommendation Submission Deadline");
		recdl_field = new JTextField();
		recdl_field.setText("Tue/12/04/16/15/00/PST/2012");

		JLabel dec_date = new JLabel("Decision Deadline");
		decdl_field = new JTextField();
		decdl_field.setText("Tue/12/04/16/15/00/PST/2012");

		JLabel loc = new JLabel("Location");
		loc_field = new JTextField();
		
		JLabel size = new JLabel("Conference Size");
		size_field = new JTextField();
		
		JButton cancel_add = new JButton(new CloseAction(my_frame));
		cancel_add.setText("Cancel");
		
		this.setLayout(exp_layout);
		this.add(conf_name);
		this.add(cname_field);
		this.add(start_date);
		this.add(sdate_field);
		this.add(end_date);
		this.add(edate_field);
		this.add(sub_date);
		this.add(subdl_field);
		this.add(rdl_date);
		this.add(revdl_field);
		this.add(recdl_date);
		this.add(recdl_field);
		this.add(dec_date);
		this.add(decdl_field);
		this.add(loc);
		this.add(loc_field);
		this.add(size);
		this.add(size_field);
		this.add(cancel_add);
		this.add(new JButton(new SaveNewConfAction()));
	}
	
	/**
	 * Class to save a new conference.
	 * @author Sarah George
	 * @version Autumn 2012
	 */
	private class SaveNewConfAction extends AbstractAction {

		/**
		 * Constructs a save a new conference action.
		 */
		public SaveNewConfAction() {
			super("Save New Conference");
			putValue(Action.MNEMONIC_KEY, KeyEvent.VK_F);
			putValue(Action.SELECTED_KEY, true);
		}

		/**
		 * Method to save a new conference.
		 * @param arg0 the action event that triggers this method.
		 */
		@Override
		public void actionPerformed(ActionEvent arg0) {
			Conference conf = new Conference(FileParser.NEW_ELEMENT, cname_field.getText(), 
					new StringToDate().convert(sdate_field.getText()),
					new StringToDate().convert(edate_field.getText()), 
					new StringToDate().convert(subdl_field.getText()),
					new StringToDate().convert(revdl_field.getText()), 
					new StringToDate().convert(recdl_field.getText()),
					new StringToDate().convert(decdl_field.getText()),
					loc_field.getText(),
					new StringToInt().convert(size_field.getText()));

			if(my_state.getParser().writeConference(conf) &&
					my_state.getParser().writeAssignment(conf.getConfID(), my_state.getUser().getID(), Role.PC))
			{
				JOptionPane.showMessageDialog(my_frame, "Conference Saved.  Please log out to view the new conference");
			} else {
				JOptionPane.showMessageDialog(my_frame, "There Was an error saving your conference.  Try again.");
			}

			my_frame.dispose();
		}
	}//End class Save New Conf Action.
}//End class new conf panel.